package sk.naive.talker.command;

import sk.naive.talker.MailHelper;
import sk.naive.talker.message.DefaultMessageFactory;

import javax.mail.*;
import javax.mail.Message;
import java.rmi.RemoteException;

/**
 * Mail list - lists messages in mail folder.
 *
 * @author <a href="mailto:virgo@naive.deepblue.sk">Richard "Virgo" Richter</a>
 * @version $Revision: 1.8 $ $Date: 2007-05-07 10:34:20 $
 */
public class MailList extends AbstractCommand {
	private MailHelper mailHelper;
	private Folder folder;
	private int startNum = 1;

	public void exec()
		throws CommandException,
		RemoteException
	{
		try {
			mailHelper = (MailHelper) getTalker().getProperties().get(MailHelper.TPROP_MAIL_HELPER);
			folder = mailHelper.getInbox(user);
			int msgCount = folder.getMessageCount();
			StringBuilder sb = new StringBuilder();
			if (params == null || params.length() == 0) {
				ctxSet(DefaultMessageFactory.CTXKEY_VAL, folder.getName());
				ctxSet(DefaultMessageFactory.CTXKEY_VAL + 1, String.valueOf(msgCount));
				ctxSet(DefaultMessageFactory.CTXKEY_VAL + 2, String.valueOf(folder.getNewMessageCount()));
				sb.append(getString("mailList.head", user.getProperties()));
				if (msgCount > 0) {
					processFolder(sb);
					sb.append(getString("mailList.tail", user.getProperties()));
				}
			} else if ("new-info".startsWith(params.toLowerCase())) {
				ctxSet(DefaultMessageFactory.CTXKEY_VAL, String.valueOf(folder.getNewMessageCount()));
				sb.append(getString("mailList.new", user.getProperties()));
			} else {
				try {
					startNum = Integer.parseInt(params);
				} catch (NumberFormatException e) {
					sendMisusageWarning();
					return;
				}
			}

			user.send(sb.toString());
			folder.close(false);
		} catch (AuthenticationFailedException e) {
			sendHelper().sendMessage(user, "mail.authenticationFailed");
		} catch (MessagingException e) {
			throw new CommandException(e);
		}
	}

	private void processFolder(StringBuilder sb) throws MessagingException {
		Message[] msgs = folder.getMessages();
		FetchProfile fp = prepareFetchProfile();
		folder.fetch(msgs, fp);
		int counter = 1;
		for (Message message : msgs) {
			if (counter < startNum) {
				counter++;
				continue;
			}
			processMessage(message, sb);
			counter++;
		}
	}

	private void processMessage(Message message, StringBuilder sb) throws MessagingException {
		ctxSet(DefaultMessageFactory.CTXKEY_VAL, mailHelper.addressString(message.getFrom()));
		ctxSet(DefaultMessageFactory.CTXKEY_VAL + 2, message.getReceivedDate());
		ctxSet(DefaultMessageFactory.CTXKEY_VAL + 3, mailHelper.flagsString(message.getFlags()));
		ctxSet(DefaultMessageFactory.CTXKEY_VAL + 4, String.valueOf(message.getMessageNumber()));
		sb.append(getString("mailList.msgLine", user.getProperties()));
		if (message.getSubject() != null && message.getSubject().length() != 0) {
			ctxSet(DefaultMessageFactory.CTXKEY_VAL + 1, message.getSubject());
			sb.append(getString("mailList.msgLineSubject", user.getProperties()));
		}
	}

	private FetchProfile prepareFetchProfile() {
		FetchProfile fp = new FetchProfile();
		fp.add(FetchProfile.Item.ENVELOPE);
		fp.add(FetchProfile.Item.FLAGS);
		fp.add("X-Mailer");
		return fp;
	}
}
